<template>
  <div class="app-container">
    <div class="upload-container">
      <h2>南宁物流文件上传中心</h2>

      <el-row :gutter="20">
        <!-- 第一个上传区域 -->
        <el-col :span="12">
          <div class="upload-area">
            <h3 class="upload-title">任务指标完成情况表</h3>
            <el-upload ref="uploadTask" :limit="1" accept=".xlsx, .xls" :headers="upload.headers"
              :action="upload.url + '?type=0'" :disabled="upload.isUploading"
              :on-progress="handleFileUploadProgressTask" :on-success="handleFileSuccessTask" :auto-upload="false"
              :on-error="handleUploadErrorTask" drag>
              <i class="el-icon-upload"></i>
              <div class="el-upload__text">将文件拖到此处，或<em>点击上传</em></div>
              <div class="el-upload__tip text-center" slot="tip">
                <div class="el-upload__tip" slot="tip">
                  <!--            <el-checkbox v-model="upload.updateSupport" />是否更新已经存在的数据-->
                </div>
                <span>仅允许导入xls、xlsx格式文件。</span>
                <!--          <el-link type="primary" :underline="false" style="font-size: 12px; vertical-align: baseline" @click="importTemplate">下载模板</el-link>-->
              </div>
            </el-upload>
            <div slot="footer" class="dialog-footer">
              <el-button type="primary" @click="submitFileFormTask">确 定</el-button>
              <el-button @click="upload.open = false">取 消</el-button>
            </div>
          </div>
        </el-col>

        <!-- 第二个上传区域 -->
        <el-col :span="12">
          <div class="upload-area">
            <h3 class="upload-title">调度日况表</h3>
            <el-upload ref="uploadDispatch" :limit="1" accept=".xlsx, .xls" :headers="upload.headers"
              :action="upload.url + '?type=1'" :disabled="upload.isUploading"
              :on-progress="handleFileUploadProgressDispatch" :on-success="handleFileSuccessDispatch"
              :auto-upload="false" drag>
              <i class="el-icon-upload"></i>
              <div class="el-upload__text">将文件拖到此处，或<em>点击上传</em></div>
              <div class="el-upload__tip text-center" slot="tip">
                <div class="el-upload__tip" slot="tip">
                  <!--            <el-checkbox v-model="upload.updateSupport" />是否更新已经存在的数据-->
                </div>
                <span>仅允许导入xls、xlsx格式文件。</span>
                <!--          <el-link type="primary" :underline="false" style="font-size: 12px; vertical-align: baseline" @click="importTemplate">下载模板</el-link>-->
              </div>
            </el-upload>
            <div slot="footer" class="dialog-footer">
              <el-button type="primary" @click="submitFileFormDispatch">确 定</el-button>
              <el-button @click="upload.open = false">取 消</el-button>
            </div>
          </div>
        </el-col>
      </el-row>

      <el-row :gutter="20" style="margin-top: 20px;">
        <!-- 第三个上传区域 -->
        <el-col :span="12">
          <div class="upload-area">
            <h3 class="upload-title">在途车预警表</h3>
            <el-upload ref="uploadOnpassage" :limit="1" accept=".xlsx, .xls" :headers="upload.headers"
              :action="upload.url + '?type=2'" :disabled="upload.isUploading"
              :on-progress="handleFileUploadProgressOnpassage" :on-success="handleFileSuccessOnpassage"
              :auto-upload="false" drag>
              <i class="el-icon-upload"></i>
              <div class="el-upload__text">将文件拖到此处，或<em>点击上传</em></div>
              <div class="el-upload__tip text-center" slot="tip">
                <div class="el-upload__tip" slot="tip">
                  <!--            <el-checkbox v-model="upload.updateSupport" />是否更新已经存在的数据-->
                </div>
                <span>仅允许导入xls、xlsx格式文件。</span>
                <!--          <el-link type="primary" :underline="false" style="font-size: 12px; vertical-align: baseline" @click="importTemplate">下载模板</el-link>-->
              </div>
            </el-upload>
            <div slot="footer" class="dialog-footer">
              <el-button type="primary" @click="submitFileFormOnpassage">确 定</el-button>
              <el-button @click="upload.open = false">取 消</el-button>
            </div>
          </div>
        </el-col>

        <!-- 第四个上传区域 -->
        <el-col :span="12">
          <div class="upload-area">
            <h3 class="upload-title">各站点承认车情况表</h3>
            <el-upload ref="uploadEach" :limit="1" accept=".xlsx, .xls" :headers="upload.headers"
              :action="upload.url + '?type=3'" :disabled="upload.isUploading"
              :on-progress="handleFileUploadProgressEach" :on-success="handleFileSuccessEach" :auto-upload="false" drag>
              <i class="el-icon-upload"></i>
              <div class="el-upload__text">将文件拖到此处，或<em>点击上传</em></div>
              <div class="el-upload__tip text-center" slot="tip">
                <div class="el-upload__tip" slot="tip">
                  <!--            <el-checkbox v-model="upload.updateSupport" />是否更新已经存在的数据-->
                </div>
                <span>仅允许导入xls、xlsx格式文件。</span>
                <!--          <el-link type="primary" :underline="false" style="font-size: 12px; vertical-align: baseline" @click="importTemplate">下载模板</el-link>-->
              </div>
            </el-upload>
            <div slot="footer" class="dialog-footer">
              <el-button type="primary" @click="submitFileFormEach">确 定</el-button>
              <el-button @click="upload.open = false">取 消</el-button>
            </div>
          </div>
        </el-col>
      </el-row>
    </div>





    <!-- <el-select v-model="upload.type" placeholder="文件类型" clearable style="width: 100%; margin-bottom: 10px">
      <el-option v-for="dict in dict.type.file_type" :key="dict.value" :label="dict.label" :value="dict.value" />
    </el-select>
    <el-upload ref="upload" :limit="1" accept=".xlsx, .xls" :headers="upload.headers"
      :action="upload.url + '?type=' + upload.type" :disabled="upload.isUploading"
      :on-progress="handleFileUploadProgress" :on-success="handleFileSuccess" :auto-upload="false" drag>
      <i class="el-icon-upload"></i>
      <div class="el-upload__text">将文件拖到此处，或<em>点击上传</em></div>
      <div class="el-upload__tip text-center" slot="tip">
        <div class="el-upload__tip" slot="tip">
   
        </div>
        <span>仅允许导入xls、xlsx格式文件。</span>
      
      </div>
    </el-upload>
    <div slot="footer" class="dialog-footer">
      <el-button type="primary" @click="submitFileForm">确 定</el-button>
      <el-button @click="upload.open = false">取 消</el-button>
    </div> -->

  </div>
</template>

<script>
import { getToken } from "@/utils/auth";

export default {
  name: "Import",
  dicts: ['file_type'],
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 任务完成情况表格数据
      importList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        type: null,
        title: null,
        num: null,
      },
      upload: {
        type: null,
        // 是否显示弹出层（用户导入）
        open: false,
        // 弹出层标题（用户导入）
        title: "",
        // 是否禁用上传
        isUploading: false,
        // 是否更新已经存在的用户数据
        updateSupport: 0,
        // 设置上传的请求头部
        headers: { Authorization: "Bearer " + getToken() },
        // 上传的地址
        url: process.env.VUE_APP_BASE_API + "/nanning/importData"
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        title: [
          { required: true, message: "标题不能为空", trigger: "blur" }
        ],
        num: [
          { required: true, message: "吨数不能为空", trigger: "blur" }
        ],
      }
    };
  },
  created() {
    console.log(this.dict.type.file_type)
  },
  methods: {

    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        type: null,
        title: null,
        num: null,
        createTime: null,
        updateTime: null
      };
      this.resetForm("form");
    },

    // 文件上传中处理
    handleFileUploadProgress(event, file, fileList) {
      this.upload.isUploading = true;
    },
    // 文件上传成功处理
    handleFileSuccess(response, file, fileList) {
      this.upload.open = false;
      this.upload.isUploading = false;
      this.$refs.upload.clearFiles();
      this.$alert("<div style='overflow: auto;overflow-x: hidden;max-height: 70vh;padding: 10px 20px 0;'>" + response.msg + "</div>", "导入结果", { dangerouslyUseHTMLString: true });
      // this.getList();
    },
    // 提交上传文件
    submitFileForm(data) {
      this.$refs.upload.submit();
    },
    //任务
    handleUploadErrorTask(err) {
      try {
        const errorData = JSON.parse(err.message)
        this.$message.error(errorData.msg || '上传失败')
      } catch {
        const errStr = err.toString().replace('Error: ', '')
        const errorData = JSON.parse(errStr)
        this.$message.error(errorData.msg)
      }
    },
    handleFileUploadProgressTask() {
      this.upload.isUploading = true;
    },
    handleFileSuccessTask(response) {
      this.upload.isUploading = false;
      this.$refs.uploadTask.clearFiles();
      if (response.code == 500) {
        this.$alert("<div style='overflow: auto;overflow-x: hidden;max-height: 70vh;padding: 10px 20px 0;'>" + '上传任务指标完成情况表失败,请确认上传的文件是否正确' + "</div>", "导入结果", { dangerouslyUseHTMLString: true });
      } else {
        this.$alert("<div style='overflow: auto;overflow-x: hidden;max-height: 70vh;padding: 10px 20px 0;'>" + response.msg + "</div>", "导入结果", { dangerouslyUseHTMLString: true });
      }

    },
    submitFileFormTask() {
      this.$refs.uploadTask.submit();
    },
    //调度
    handleFileUploadProgressDispatch() {
      this.upload.isUploading = true;
    },
    handleFileSuccessDispatch(response) {
      this.upload.isUploading = false;
      this.$refs.uploadDispatch.clearFiles();
      if (response.code == 500) {
        this.$alert("<div style='overflow: auto;overflow-x: hidden;max-height: 70vh;padding: 10px 20px 0;'>" + '上传调度日况表失败,请确认上传的文件是否正确' + "</div>", "导入结果", { dangerouslyUseHTMLString: true });
      } else {
        this.$alert("<div style='overflow: auto;overflow-x: hidden;max-height: 70vh;padding: 10px 20px 0;'>" + response.msg + "</div>", "导入结果", { dangerouslyUseHTMLString: true });
      }
    },
    submitFileFormDispatch() {
      this.$refs.uploadDispatch.submit();
    },
    //在途车
    handleFileUploadProgressOnpassage() {
      this.upload.isUploading = true;
    },
    handleFileSuccessOnpassage(response) {
      this.upload.isUploading = false;
      this.$refs.uploadOnpassage.clearFiles();
      if (response.code == 500) {
        this.$alert("<div style='overflow: auto;overflow-x: hidden;max-height: 70vh;padding: 10px 20px 0;'>" + '上传在途车预警表失败,请确认上传的文件是否正确' + "</div>", "导入结果", { dangerouslyUseHTMLString: true });
      } else {
        this.$alert("<div style='overflow: auto;overflow-x: hidden;max-height: 70vh;padding: 10px 20px 0;'>" + response.msg + "</div>", "导入结果", { dangerouslyUseHTMLString: true });
      }
    },
    submitFileFormOnpassage() {
      this.$refs.uploadOnpassage.submit();
    },
    //各站点
    handleFileUploadProgressEach() {
      this.upload.isUploading = true;
    },
    handleFileSuccessEach(response) {
      this.upload.isUploading = false;
      this.$refs.uploadEach.clearFiles();
      if (response.code == 500) {
        this.$alert("<div style='overflow: auto;overflow-x: hidden;max-height: 70vh;padding: 10px 20px 0;'>" + '上传各站点承认车情况表失败,请确认上传的文件是否正确' + "</div>", "导入结果", { dangerouslyUseHTMLString: true });
      } else {
        this.$alert("<div style='overflow: auto;overflow-x: hidden;max-height: 70vh;padding: 10px 20px 0;'>" + response.msg + "</div>", "导入结果", { dangerouslyUseHTMLString: true });
      }
    },
    submitFileFormEach() {
      this.$refs.uploadEach.submit();
    }

  }
};
</script>
<style>
.upload-container {
  padding: 20px;
  max-width: 1200px;
  margin: 0 auto;
}

.upload-area {
  border: 1px solid #ebeef5;
  border-radius: 4px;
  padding: 20px;
  background-color: #fff;
}

.upload-title {
  margin: 0 0 15px 0;
  color: #303133;
  font-size: 16px;
  font-weight: bold;
}

.el-upload__tip {
  margin-top: 10px;
  color: #909399;
  font-size: 12px;
}

h2 {
  text-align: center;
  margin-bottom: 30px;
  color: #409EFF;
}
</style>